package org.scala.javafx.rich

import java.lang.reflect.Method

case class RichMethod(underlying: Method) {
  def name = underlying.getName()
  def parameterTypes = underlying.getParameterTypes()
  def returnType = underlying.getReturnType()
  def apply(item: Object)(parameters: Object*): Object = {
    val array = parameters.toArray
    if (array.size > 0) {
      underlying.invoke(item, parameters.toArray)
    } else {
      underlying.invoke(item)
    }
  }
  def isGetter: Boolean = parameterTypes.size == 0 && name.startsWith("get") && name.length > 3
  def isSetter: Boolean = parameterTypes.size == 1 && name.startsWith("set") && name.length > 3
  def getterName: Option[String] = {
    if (isGetter) Some(name.charAt(3).toLower + name.substring(4)) else None
  }
  def setterName: Option[String] = {
    if (isSetter) Some(name.charAt(3).toLower + name.substring(4)) else None
  }
}
